<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>/net/nas/exports/srv/www/webapps/Projects/Java/CS222/Seminar_3/AssignmentTwo/src/IO.java.html</title>
<meta name="Generator" content="Vim/7.1">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body bgcolor="#ffffff" text="#000000"><font face="monospace">
<font color="#0000ff">/**</font><br>
<font color="#0000ff">&nbsp;*</font><font color="#6a5acd">&nbsp;Assignment Two</font><br>
<font color="#0000ff">&nbsp;*</font><font color="#6a5acd">&nbsp;Analyze the code presented in the IO program.</font><br>
<font color="#0000ff">&nbsp;* Edit the code to run for lowercase letters “a-e”.</font><br>
<font color="#0000ff">&nbsp;*</font><br>
<font color="#0000ff">&nbsp;* </font><font color="#6a5acd">@author</font><font color="#0000ff">: Unknown</font><br>
<font color="#0000ff">&nbsp;*&nbsp;&nbsp;Editor: John Thomason</font><br>
<font color="#0000ff">&nbsp;*&nbsp;&nbsp;Course: CS222 Java II</font><br>
<font color="#0000ff">&nbsp;* Seminar: 3</font><br>
<font color="#0000ff">&nbsp;*&nbsp;&nbsp;Assign: 2</font><br>
<font color="#0000ff">&nbsp;*/</font><br>
<font color="#a020f0">import</font>&nbsp;java.io.*;<br>
<br>
<font color="#2e8b57"><b>public</b></font>&nbsp;<font color="#2e8b57"><b>class</b></font>&nbsp;IO {<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2e8b57"><b>public</b></font>&nbsp;<font color="#2e8b57"><b>static</b></font>&nbsp;<font color="#2e8b57"><b>void</b></font>&nbsp;main(String[]&nbsp;Jim)&nbsp;<font color="#2e8b57"><b>throws</b></font>&nbsp;IOException {<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">//Declaring variables</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BufferedReader dataIn = <font color="#a52a2a"><b>new</b></font>&nbsp;BufferedReader(<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#a52a2a"><b>new</b></font>&nbsp;InputStreamReader(System.in));<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2e8b57"><b>int</b></font>&nbsp;num = <font color="#ff00ff">0</font>;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2e8b57"><b>char</b></font>&nbsp;begin&nbsp;&nbsp; = <font color="#ff00ff">'a'</font>;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#0000ff">//Beginning character</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2e8b57"><b>char</b></font>&nbsp;end&nbsp;&nbsp;&nbsp;&nbsp; = <font color="#ff00ff">'e'</font>;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#0000ff">//Ending character</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String decNum;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String binNum;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2e8b57"><b>boolean</b></font>&nbsp;done = <font color="#ff00ff">false</font>;<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">//Main Loop to get and validate user input</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#a52a2a"><b>do</b></font>&nbsp;{<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#a52a2a"><b>try</b></font>&nbsp;{ <font color="#0000ff">//to get valid user input</font><br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;displayMenu(begin,end);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;decNum = dataIn.readLine();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num = Integer.parseInt(decNum);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;validate(num,begin,end);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;done = <font color="#ff00ff">true</font>;<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<font color="#0000ff">//try</font><br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#a52a2a"><b>catch</b></font>(NumberFormatException e)&nbsp;{ <font color="#0000ff">//User made a Boo Boo!</font><br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prt(<font color="#ff00ff">&quot;</font><font color="#6a5acd">\n</font><font color="#ff00ff">Your response was not a valid number.</font><font color="#6a5acd">\n</font><font color="#ff00ff">&quot;</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prt(<font color="#ff00ff">&quot;Please reenter using the allowable numeric values.</font><font color="#6a5acd">\n</font><font color="#ff00ff">&quot;</font>);<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<font color="#0000ff">//catch()</font><br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <font color="#a52a2a"><b>while</b></font>(!done);<font color="#0000ff">//do/while loop</font><br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">//Convert the decimal number to a binary string</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;binNum = getBinary(num);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">//Display Output</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prt(String.format(<font color="#ff00ff">&quot;</font><font color="#6a5acd">\n</font><font color="#ff00ff">The binary equivalent of %d is %s</font><font color="#6a5acd">\n\n</font><font color="#ff00ff">&quot;</font>,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; num, binNum));<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;} <font color="#0000ff">//end main</font><br>
<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">//PRIVATE SUPPORTING METHODS</font><br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">/**</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; *</font><font color="#6a5acd">&nbsp;Display the menu to prompt the user for input</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; *</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#0000ff">* </font><font color="#6a5acd">@param</font><font color="#008b8b">&nbsp;begin</font><font color="#0000ff">&nbsp;The first menu item</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * </font><font color="#6a5acd">@param</font><font color="#008b8b">&nbsp;end</font><font color="#0000ff">&nbsp;&nbsp; The last menu item</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; */</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2e8b57"><b>private</b></font>&nbsp;<font color="#2e8b57"><b>static</b></font>&nbsp;<font color="#2e8b57"><b>void</b></font>&nbsp;displayMenu(<font color="#2e8b57"><b>char</b></font>&nbsp;begin, <font color="#2e8b57"><b>char</b></font>&nbsp;end){<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">//Prompt the user for input</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prt(<font color="#ff00ff">&quot;</font><font color="#6a5acd">\n</font><font color="#ff00ff">Enter the decimal number for the ascii letter</font><font color="#6a5acd">\n</font><font color="#ff00ff">&quot;</font>);<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">//Display integer values and characters from begin to end</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#a52a2a"><b>for</b></font>(&nbsp;<font color="#2e8b57"><b>char</b></font>&nbsp;c = begin; c &lt;= end; c++ ){<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prt(String.format(<font color="#ff00ff">&quot;</font><font color="#6a5acd">\t</font><font color="#ff00ff">%3d) %c</font><font color="#6a5acd">\n</font><font color="#ff00ff">&quot;</font>, (<font color="#2e8b57"><b>int</b></font>)c, c));<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<font color="#0000ff">//for()</font><br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">//Display final prompt</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prt(<font color="#ff00ff">&quot;Enter Number: &quot;</font>);<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<font color="#0000ff">//displayMenu()</font><br>
<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">/**</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; *</font><font color="#6a5acd">&nbsp;Validate the user input</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; *</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#0000ff">* </font><font color="#6a5acd">@param</font><font color="#008b8b">&nbsp;num</font><font color="#0000ff">&nbsp;&nbsp; The user entered value</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * </font><font color="#6a5acd">@param</font><font color="#008b8b">&nbsp;begin</font><font color="#0000ff">&nbsp;The beginning item in the acceptable range</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * </font><font color="#6a5acd">@param</font><font color="#008b8b">&nbsp;end</font><font color="#0000ff">&nbsp;&nbsp; The ending item in the acceptable range</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * </font><font color="#6a5acd">@throws</font><font color="#008b8b">&nbsp;java.lang.NumberFormatException</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; */</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2e8b57"><b>private</b></font>&nbsp;<font color="#2e8b57"><b>static</b></font>&nbsp;<font color="#2e8b57"><b>void</b></font>&nbsp;validate(<font color="#2e8b57"><b>int</b></font>&nbsp;num, <font color="#2e8b57"><b>char</b></font>&nbsp;begin, <font color="#2e8b57"><b>char</b></font>&nbsp;end)<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2e8b57"><b>throws</b></font>&nbsp;NumberFormatException {<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">//Validate the number entered is within character range</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#a52a2a"><b>if</b></font>(&nbsp;num &lt; (<font color="#2e8b57"><b>int</b></font>)begin || num &gt; (<font color="#2e8b57"><b>int</b></font>)end ){<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prt(String.format(<font color="#ff00ff">&quot;</font><font color="#6a5acd">\n</font><font color="#ff00ff">The number must be between %d and %d.&quot;</font>,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (<font color="#2e8b57"><b>int</b></font>)begin, (<font color="#2e8b57"><b>int</b></font>)end));<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#a52a2a"><b>throw</b></font>&nbsp;<font color="#a52a2a"><b>new</b></font>&nbsp;NumberFormatException();<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<font color="#0000ff">// end if</font><br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<font color="#0000ff">//validate()</font><br>
<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">/**</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; *</font><font color="#6a5acd">&nbsp;Converts a decimal value less than or equal to 256 to</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; *</font><font color="#6a5acd">&nbsp;a 8 character string representing it's binary equivalent.</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; *</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * </font><font color="#6a5acd">@param</font><font color="#008b8b">&nbsp;findBin</font><font color="#0000ff">&nbsp;&nbsp; The decimal value to convert</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; * </font><font color="#6a5acd">@return</font><font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A string representing the binary value</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; */</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2e8b57"><b>private</b></font>&nbsp;<font color="#2e8b57"><b>static</b></font>&nbsp;String getBinary(<font color="#2e8b57"><b>int</b></font>&nbsp;findBin){<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">//StringBuilder used because it's mutable</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StringBuilder theBinary = <font color="#a52a2a"><b>new</b></font>&nbsp;StringBuilder();<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">//Build the binary string in reverse</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#a52a2a"><b>while</b></font>(findBin &gt; <font color="#ff00ff">0</font>){<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theBinary.append(findBin % <font color="#ff00ff">2</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;findBin /= <font color="#ff00ff">2</font>;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<font color="#0000ff">//while()</font><br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">//Pad to 8 bit length</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#a52a2a"><b>while</b></font>(theBinary.length()&nbsp;&lt; <font color="#ff00ff">8</font>){ theBinary.append(<font color="#ff00ff">&quot;0&quot;</font>); }<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">//reverse the StringBuilder array and return as a String</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#a52a2a"><b>return</b></font>&nbsp;theBinary.reverse().toString();<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<font color="#0000ff">// end getBinary</font><br>
<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">/**</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; *</font><font color="#6a5acd">&nbsp;Display a formatted message string</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; *</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#0000ff">* </font><font color="#6a5acd">@param</font><font color="#008b8b">&nbsp;msg</font><font color="#0000ff">&nbsp;&nbsp; The formatted message string</font><br>
<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp; */</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2e8b57"><b>private</b></font>&nbsp;<font color="#2e8b57"><b>static</b></font>&nbsp;<font color="#2e8b57"><b>void</b></font>&nbsp;prt(String msg){<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print(msg);<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<font color="#0000ff">//prt</font><br>
<br>
} <font color="#0000ff">//End IO class</font><br>
</font></body>
</html>
